﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Teamwish.Domain.Common;
using Teamwish.Domain.Models;
using Teamwish.WebUI.Filters;

namespace Teamwish.WebUI.Areas.Admin.Controllers
{
    [AdminAuthorize(Roles = "管理员")]
    public class HomeController : Controller
    {
        /// <summary>
        /// 数据库上下文
        /// </summary>
        private EfDbContext db = DIFactory.Resolve<EfDbContext>();
        /// <summary>
        /// 后台主框架页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Main()
        {
            var dd = this;
            return View();
        }

        /// <summary>
        /// 欢迎页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Welcome()
        {
            return View();
        }

        /// <summary>
        /// 管理员登录页面
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        public ActionResult Login()
        {
            return View();
        }

        /// <summary>
        /// 处理管理员登录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        [AllowAnonymous]
        public ActionResult Login(string LoginName, string Pwd)
        {
            ViewBag.LoginName = LoginName;
            ViewBag.Pwd = Pwd;
            if (string.IsNullOrWhiteSpace(LoginName))
            {
                TempData["msg"] = "用户名不能为空";
                return View();
            }
            if (string.IsNullOrWhiteSpace(Pwd))
            {
                TempData["msg"] = "密码不能为空";
                return View();
            }
            string pwd = Teamwish.Utilities.Md5Helper.MD5(Pwd,32);
            Userinfo userinfo = db.Userinfo.AsNoTracking().Where(p => p.Username == LoginName && p.Password == pwd).SingleOrDefault();
            if ((LoginName == "Admin" && Pwd == "123456") || (userinfo != null && userinfo.Role != null))
            {
                string roles;
                if (LoginName == "Admin")
                {
                    roles = "管理员";
                }
                else {
                    roles = userinfo.Role.ToString();
                }
               
                //生成验证票据对象．
                FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, LoginName, DateTime.Now, DateTime.Now.AddMinutes(20), false, roles);
                //加密验证票
                string encrytedTicket = FormsAuthentication.Encrypt(authTicket);
                //生成Cookie对象．
                //FormsAuthentication.FormsCookieName取得WebConfig中<Authentication>
                //配置节中Name的值作为Cookie的名字．
                HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encrytedTicket);
                Response.Cookies.Add(authCookie);

                return RedirectToAction(nameof(Main));
            }
            else
            {
                TempData["msg"] = "用户名或密码错误";
            }

            return View();
        }


        /// <summary>
        /// 管理员退出
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            //FormsAuthentication.RedirectToLoginPage();
            return RedirectToAction(nameof(Login));
        }
    }
}